<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Zs_order_pay;

use Think\Log;

class Zs_order_pay extends \Service\Common\BaseModel
{
    protected $name = 'Zs_order_pay';
    protected $is_vip_t = 1;
    protected $is_vip_f = 0;

    public function formatCallback($data)
    {
        $order = $this->where(['id' => $data['zs_order_pay.id']])->find();
        $goods = M('zs_goods')->where(['skuId'=>$order['skuId']])->find();
        $activity = M('zs_activity')->where(['taobao_act_id' => $order['taobao_act_id']])->find();
        if($goods){
            $goodsinfo = "<div style='word-wrap: break-word;width: 180px; height: 70px;word-break:normal;white-space: nowrap;overflow:hidden;text-overflow:ellipsis'><a href='{$goods['goods_url']}'>".$order["skuName"]
                ."</a><br/>商品id：".$order["skuId"]."<br/>".$activity['activity_title']."</div>";
        }else{
            $goodsinfo = "<div style='word-wrap: break-word;width: 180px; height: 70px;word-break:normal;white-space: nowrap;overflow:hidden;text-overflow:ellipsis '>".$order["skuName"].
                "<br/>商品id：".$order["skuId"]."</div>";
        }

        $img = "<a href='{$goods['goods_url']}' target='_blank'><img src='{$goods['imgUrl']}' style='width: 60px;'></a>";

        return [
            'zs_order_pay.img_logo' => $img,
            'zs_order_pay.goods_info'=>$goodsinfo,
//            'zs_order_pay.yugu_money'=>$order['price']*$goods['tuanzhang_radio'],
        ];
    }
    
    public function getTypeList()
    {
    	$list = M('poster')->where(['category'=>'goods_category'])
    	                   ->order('level asc,id asc')
    	                   ->select();
    	if ($list) {
    		foreach ($list as $k=>$v){
    			$list[$k]['pic_url'] = M('picture')->where(['id'=>$v['pic']])
    	                                           ->getField('url');
    		}
    	}
    	return $list;
    }
    //获取商品详情
    public function getDetail($goods_id, $uid = 0)
    {
        $result = $this->find($goods_id);
       
        if (!$result) {
            throw new \Exception('没有该商品');
        }
        return $this->format($result, $uid);
    }

    //搜索
    public function search($keyword)
    {
        $list = $this->where([
            'title' => ['like', "%{$keyword}%"],
            'intro' => ['like', "%{$keyword}%"],
            '_logic' => 'OR'
        ])->select();
        return $this->format($list);
    }

    //格式化
    public function format($list, $uid = 0)
    {
        if (!$list) {
            return $list;
        }

        $is_one = count($list) == count($list, 1);
        $list = $is_one ? [$list] : $list;

        foreach ($list as &$row) {
            //复购价格
            $row['is_rebuy'] = 0;
            if ($uid) {
                $is_buy = M('user')->where(['uid' => $uid])->getField('vip_level') >= 1;
                $is_buy AND $row['contribution'] = $row['rebuy_contribution'];
                $is_buy AND $row['is_rebuy'] = 1;
            }

            $row['list_cover'] = get_cover($row['list_cover'], 'path');
            $picture_ids = array_filter(explode(',', $row['picture_ids']));
            $row['picture_urls'] = [];
            foreach ($picture_ids as $id) {
                $row['picture_urls'] [] = get_cover($id, 'path');
            }
        }
        return $is_one ? $list[0] : $list;
    }
}